import CryptoJS from 'crypto-js';

export function encrypt() {
	let keyStr = "QURFNjMwQjgwRTA2RTEyOQ==";
	keyStr = new Buffer(keyStr, 'base64').toString('utf8')
	// keyStr = atob(keyStr);
	let ivStr = getRanHex(16);
	let noIv = ivStr
	let word = getWord();
	let key = CryptoJS.enc.Utf8.parse(keyStr);
	let iv = CryptoJS.enc.Utf8.parse(ivStr);
	let srcs = CryptoJS.enc.Utf8.parse(word);
	let encrypted = CryptoJS.AES.encrypt(srcs, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7,
	});
	return ivStr + encrypted.toString();
}
// 生成16位乱码字符串
function getRanHex(size) {
	let result = [];
	let hexRef = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
		'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
	];
	for (let n = 0; n < size; n++) {
		result.push(hexRef[Math.floor(Math.random() * 16)]);
	}
	return result.join('');
}
export function getWord() {
	//wwd 平台标识 +2位平台 01,02,03 +yyyyMMddhhmmss秘钥格式
	let PlatformIdentification = "wwd";
	let device = "01"; //平台
	let timeString = "";
	let date = new Date() // 获取时间
	let year = date.getFullYear() // 获取年
	let month = date.getMonth() + 1 // 获取月
	if (month < 10) month = `0${month}`;
	let strDate = date.getDate() // 获取日
	if (strDate < 10) strDate = `0${strDate}`;
	let hour = date.getHours();
	if (hour < 10) hour = `0${hour}`;
	let minute = date.getMinutes();
	if (minute < 10) minute = `0${minute}`;
	let second = date.getSeconds();
	if (second < 10) second = `0${second}`;

	timeString = `${year}${month}${strDate}${hour}${minute}${second}`
	return `${PlatformIdentification}${device}${timeString}`
}